<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSunmit">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles, assignRoles } from '@/api/employees'
import { upInfo } from '@/api/user'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },

  created() {
    this.lodeRole()
  },

  methods: {
    // 分配角色
    async hSunmit() {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        // 通知父组件关闭弹层
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 根据ID获取角色详情
    async lodeRole() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        const defId = await upInfo(this.curId)
        // console.log(defId)
        this.roleIds = defId.data.roleIds
        // 保存数据
        if (res.code !== 10000) return this.$message.error(res.message)
        this.list = res.data.rows
      } catch (err) {
        console.log('getRole', err)
      }
    }

  }
}
</script>
